#ifndef _GsMusic_Player_Impl_H_
#define _GsMusic_Player_Impl_H_

#include <Windows.h>
#include <string>
#include "libzplay.h"

using namespace GlsxSound;

struct read_write_buffer;

typedef void(*PlayStateEvent_Impl)(UINT uMsg, LPARAM lParam, WPARAM wParam, void* pUserData);

class GsMusicPlayerImpl
{
public:
	ZPlay* m_player;
public:
	GsMusicPlayerImpl();
	~GsMusicPlayerImpl();

	int Init_Impl(PlayStateEvent_Impl pStateEvent, void* pUserDate);

	int DeInit_Impl();

	int Play_Impl(const std::string& strFilePath, const std::string& strUrl = "");

	int Play2_Impl(const char* pData, int nSize);

	int Stop_Impl();

	int Pause_Impl();

	int Resum_Impl();

	int SetVol_Impl(int nVol);

	int GetPlayState_Impl();

private:
	int Connect(const std::string& strUrl, const std::string& strCustomHeader = "");

	int ProcessDownloadFileComplete();

	int ReadResponseHeader();

protected:
	static DWORD WINAPI OpenUrlThread(LPVOID pUserData);

	static DWORD WINAPI StateCallBackThread(LPVOID pUserData);

	static int  __stdcall  MyCallbackFunc(void* instance, void *user_data,TCallbackMessage message, 
		unsigned int param1, unsigned int param2);

private:
	void* m_pUserData;
	PlayStateEvent_Impl m_pStateEvent;

	DWORD m_dwStateEventTid;
	HANDLE m_stateEventThread;

	HANDLE m_openUrlThread;	

	bool m_bExit;
	bool m_bIsLocalFile;

	std::string m_strUrl;
	std::string m_strFileName;

	CRITICAL_SECTION c_cs;

	
	FILE* m_pf;
	int m_socket;

	int m_nTotalDuration;
	int m_nFileSize;
	int m_nCurReadPos;
	int m_nCurWritePos;

	read_write_buffer* m_pRwb;

};

#endif